;
; ------------------------------------------------------------
;
;   PureBasic - Windowed Screen example file
;
;    (c) 2006 - Fantaisie Software
;
; ------------------------------------------------------------
;

If InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester("Error", "Can't open DirectX 7 or later", 0)
  End
EndIf

If OpenWindow(0,0,0,410,310,"A screen in a window... using gadget and sprites!",#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
  If CreateGadgetList(WindowID(0))
    ButtonGadget(1,340, 10,60,20,"Button 1")
    ButtonGadget(2,340, 40,60,20,"Button 2")
    ButtonGadget(3,340, 70,60,20,"Button 3")
    TextGadget  (4,340,110,60,30,"")
  EndIf

  If OpenWindowedScreen(WindowID(0),5,5,320,300,0,0,0)
    CreateSprite(0,20,20)
    ; Draw some red line on our sprite
    If StartDrawing(SpriteOutput(0))
      FrontColor(RGB(255,0,0))
      For k = 0 To SpriteHeight(0) Step 2
        Line(0, k, SpriteWidth(0), 0)
      Next
      StopDrawing()
    EndIf
    CopySprite(0,1)
    CopySprite(0,2)
  Else
    MessageRequester("Error", "Can't open windowed screen!", 0)
    End
  EndIf

EndIf

direction = 1
playerX = 1
playerY = 1

Repeat
  Event.l = WindowEvent()    
  
  If Event = #PB_Event_Gadget
    ; do the normal application stuff here...
    Gadget  = EventGadget()  

    Select Gadget
      Case 1, 2, 3
        SetGadgetText(4,"Button "+Str(Gadget)+" pressed.")
    EndSelect
  Else
    ; do the sprite & screen stuff here...
    FlipBuffers()       ; Inverse the buffers (the back become the front (visible)... and we can do the rendering on the back
    ExamineKeyboard()
    If KeyboardPushed(#PB_Key_Up)    And playerY > 0   : playerY -3 : EndIf  
    If KeyboardPushed(#PB_Key_Down)  And playerY < 280 : playerY +3 : EndIf  
    If KeyboardPushed(#PB_Key_Left)  And playerX > 0   : playerX -3 : EndIf  
    If KeyboardPushed(#PB_Key_Right) And playerX < 300 : playerX +3 : EndIf  
    
    ; Clear the screen and draw our sprites
    ClearScreen(RGB(0,0,0))
    ClipSprite(0, 0, 0, x, x/8)
    DisplaySprite(0, x, 100)
    DisplaySprite(1, x, x)
    DisplaySprite(0, 300-x, x)
    DisplaySprite(2, playerX, playerY)

    x + direction
    If x > 300 : direction = -1 : EndIf   ; moving back to the left with negative value
    If x < 0   : direction =  1 : EndIf   ; moving to the right with positive value
    
    Delay(1)
  EndIf

Until Event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)

End   
